package com.supermap.application.configuration;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.persistence.EntityManager;
import javax.sql.DataSource;
import java.util.Map;

/**
 * @name 第二数据源jpa配置
 * @author sun'f
 * @date 2019-02-28
 */
/*@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        entityManagerFactoryRef = "industryEntityManagerFactory",
        transactionManagerRef = "industryTransactionManager",
        basePackages = {"com.supermap.application.industry.entity"}
)*/
public class IndustryDataSourceJpaConfig {

   /* @Autowired
    @Qualifier("industryDataSource")
    private DataSource industryDataSource;

    @Bean(name = "industryEntityManager")
    public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
        return industryEntityManagerFactory(builder).getObject().createEntityManager();
    }

    @Bean(name = "industryEntityManagerFactory")
    public LocalContainerEntityManagerFactoryBean industryEntityManagerFactory (EntityManagerFactoryBuilder builder) {
        return builder
                .dataSource(industryDataSource)
                .properties(getVendorProperties(industryDataSource))
                .packages("com.supermap.application.industry.entity") //设置实体类所在位置
                .persistenceUnit("industryPersistenceUnit")
                .build();
    }

    @Autowired(required = false)
    private JpaProperties jpaProperties;

    private Map<String, String> getVendorProperties(DataSource dataSource) {
        return jpaProperties.getHibernateProperties(dataSource);
    }

    @Bean(name = "industryTransactionManager")
    PlatformTransactionManager industryTransactionManager(EntityManagerFactoryBuilder builder) {
        return new JpaTransactionManager(industryEntityManagerFactory(builder).getObject());
    }*/

}
